Custom Exceptions
In Java, you can define your own exceptions by creating a subclass of the Exception class or its subclasses. These are called custom exceptions and are useful for signaling specific error conditions that are unique to your application.
In this section, weβll explore:
- Why and when to use custom exceptions.
- How to create and throw custom exceptions.
- Practical examples of using custom exceptions.
Why Use Custom Exceptions?β
Custom exceptions allow you to:
- Signal Specific Errors:
- Provide meaningful error messages tailored to your application's logic.
- Improve Code Readability:
- Make your code more expressive by using descriptive exception names.
- Handle Unique Scenarios:
- Handle error conditions that are not covered by built-in exceptions.
For example:
- A banking application might throw a
NegativeBalanceExceptionif a user tries to withdraw more money than their account balance. - A test automation framework might throw a
TestValidationExceptionif a validation step fails.
Creating Custom Exceptionsβ
To create a custom exception, extend the Exception class (for checked exceptions) or the RuntimeException class (for unchecked exceptions).
Syntax for Custom Exceptionsβ
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
Example: Creating a Checked Custom Exceptionβ
// Custom checked exception
public class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
// Using the custom exception
public class CustomExceptionExample {
public static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above.");
} else {
System.out.println("Valid age.");
}
}
public static void main(String[] args) {
try {
validateAge(16); // This will throw InvalidAgeException
} catch (InvalidAgeException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: Age must be 18 or above.
Explanation:
- The
InvalidAgeExceptionclass extendsException, making it a checked exception. - The
validateAgemethod throws the custom exception if the age is less than 18. - The
catchblock in themainmethod handles the exception.
Creating Unchecked Custom Exceptionsβ
Unchecked custom exceptions extend the RuntimeException class. These exceptions do not need to be declared in the method signature or handled explicitly.
Example: Creating an Unchecked Custom Exceptionβ
// Custom unchecked exception
public class NegativeNumberException extends RuntimeException {
public NegativeNumberException(String message) {
super(message);
}
}
// Using the custom exception
public class UncheckedCustomExceptionExample {
public static void checkNumber(int number) {
if (number < 0) {
throw new NegativeNumberException("Number cannot be negative.");
} else {
System.out.println("Valid number.");
}
}
public static void main(String[] args) {
try {
checkNumber(-5); // This will throw NegativeNumberException
} catch (NegativeNumberException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: Number cannot be negative.
Explanation:
- The
NegativeNumberExceptionclass extendsRuntimeException, making it an unchecked exception. - The
checkNumbermethod throws the custom exception if the number is negative. - The
catchblock in themainmethod handles the exception.
Best Practices for Custom Exceptionsβ
-
Use Descriptive Names:
- Name your custom exceptions clearly to indicate the specific error condition (e.g.,
InvalidAgeException,NegativeBalanceException).
- Name your custom exceptions clearly to indicate the specific error condition (e.g.,
-
Provide Meaningful Error Messages:
- Include detailed error messages to help developers understand the cause of the exception.
throw new InvalidAgeException("User age is below the minimum allowed limit."); -
Choose Between Checked and Unchecked Exceptions Wisely:
- Use checked exceptions for recoverable errors that the caller should handle.
- Use unchecked exceptions for programming errors or unrecoverable conditions.
-
Avoid Overusing Custom Exceptions:
- Only create custom exceptions when built-in exceptions are insufficient to describe the error condition.
Practical Example: Custom Exception in Test Automationβ
Hereβs an example of using a custom exception in a test automation framework:
// Custom exception for test validation
public class TestValidationException extends Exception {
public TestValidationException(String message) {
super(message);
}
}
// Using the custom exception in a test
public class TestAutomationExample {
public static void validateLoginButtonVisibility(boolean isVisible) throws TestValidationException {
if (!isVisible) {
throw new TestValidationException("Login button is not visible.");
} else {
System.out.println("Login button is visible.");
}
}
public static void main(String[] args) {
try {
boolean loginButtonVisible = false; // Simulate a hidden login button
validateLoginButtonVisibility(loginButtonVisible);
} catch (TestValidationException e) {
System.out.println("Test failed: " + e.getMessage());
}
}
}
Output:
Test failed: Login button is not visible.
Explanation:
- The
TestValidationExceptionclass is used to signal a test failure due to a missing login button. - The
validateLoginButtonVisibilitymethod throws the custom exception if the login button is not visible.
Key Takeawaysβ
- Custom exceptions allow you to define specific error conditions that are unique to your application.
- To create a custom exception, extend the
Exceptionclass (for checked exceptions) or theRuntimeExceptionclass (for unchecked exceptions). - Best practices include using descriptive names, providing meaningful error messages, and choosing between checked and unchecked exceptions wisely.
- Custom exceptions are particularly useful in scenarios like test automation, where specific validation failures need to be signaled.